#include <sxAnimationStudio.h>
#include <sxQViewportFrame.h>
#include <sxQSquareFrameLayout.h>
#include <sxQViewportFactory.h>
#include <sxOpenGL_DefaultCameraRenderer.h>
#include <sxQCameraFrame.h>
#include <sxQConsole.h>
#include <sxAnimationStudioStyles.h>
#include <sxOpenGL_PickingController.h>
#include <sxOpenGL_Canvas.h>

/* Qt Includes */
#include <QGridLayout>

using Sx::QtInterface::QViewportFrame;
using Sx::QtInterface::QOpenGL_Viewport;
using Sx::QtInterface::QViewportFactory;
using Sx::Interface::ViewController;
using Sx::OpenGL::Graphics::Objects::Rendering::OpenGL_DefaultCameraRenderer;
using Sx::QtInterface::QCameraFrame;
using Sx::QtInterface::System::QConsole;

using Sx::OpenGL::Graphics::Picking::OpenGL_PickingController;
using Sx::OpenGL::Interface::OpenGL_Canvas;

Sx::AnimationStudio::AnimationStudio(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) {
	ui.setupUi(this);

	this->openglContext = std::make_shared<OpenGL_Context>();

	this->constructInterface();
}

Sx::AnimationStudio::~AnimationStudio() {
	
}

bool Sx::AnimationStudio::constructInterface() {
	this->constructConsoles();
	this->constructMainLayout();
	return true;
}

bool Sx::AnimationStudio::constructMainLayout() {

	this->squareLayout = new QtInterface::QSquareFrameLayout();

	unsigned int viewportCount = this->squareLayout->getRequiredViewportCount();
	this->sceneControllers.resize(viewportCount);
	Util::ArrayList<QViewportFrame*> viewports(viewportCount);

	for ( unsigned int i = 0; i < viewportCount; i++ ) {
		this->sceneControllers[i] = std::make_shared<SceneController>(nullptr, std::make_shared<OpenGL_Canvas>(this->openglContext), std::make_shared<OpenGL_PickingController>(), std::make_shared<OpenGL_DefaultCameraRenderer>(this->openglContext));
		QCameraFrame* cf = QViewportFactory<QCameraFrame, OpenGL_Context, QOpenGL_Viewport>::getInstance().createViewportFrame(this->ui.viewportContainer, this->openglContext, this->sceneControllers[i]);
		cf->setCameraView(this->sceneControllers[i]);
		if ( i == 3 ) cf->setClearColor(0.15f, 0.15f, 0.15f);
		else cf->setClearColor(0.19f, 0.19f, 0.19f);
		cf->setStyleSheet(Style::VIEWPORT_FRAME_STYLE);
		viewports[i] = cf;
	}
	
	this->squareLayout->setViewportFrames(viewports);
	
	QGridLayout* viewportContainerLayout = new QGridLayout(this->ui.viewportContainer);
	viewportContainerLayout->setContentsMargins(0, 0, 0, 0);
	viewportContainerLayout->addWidget(this->squareLayout);
	return true;
}

bool Sx::AnimationStudio::constructConsoles() {
	QGridLayout* outLayout = new QGridLayout(this->ui.consoleContainer);
	outLayout->setContentsMargins(0, 0, 0, 0);
	QConsole* out = new QConsole(this, &std::cout, "Sx-AnimationStudio-out.log");
	out->setObjectName("out");
	out->setStyleSheet(Style::CONSOLE_OUTPUT_STYLE);
	QConsole* err = new QConsole(this, &std::cerr, "Sx-AnimationStudio-err.log");
	err->setObjectName("out");
	err->setStyleSheet(Style::CONSOLE_OUTPUT_STYLE);
	outLayout->addWidget(out, 0, 0);
	outLayout->addWidget(err, 0, 1);
	return true;
}
